﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SIGCBI.Entities;
using System.Data;
using System.Data.Entity;
using System.Data.Objects;

namespace SIGCBI.Repository
{
    public interface IGuiaEntradaRepository
    {
        List<GUIA_ENTRADA> GetAllFromGuiaEntrada();
        List<GUIA_ENTRADA> GetFromGuiaEntradaByCriterioAndAreaAndFecha(string proveedor, string nroDoc, string area, DateTime? fec1, DateTime? fec2);

        GUIA_ENTRADA GetFromGuiaEntradaById(Int32 id);

        void AddGuiaEntrada(GUIA_ENTRADA guiaEntrada);
        void UpdateGuiaEntrada(GUIA_ENTRADA guiaEntrada);

        bool GetAllGuiaEntradasByProveedorId(int idProveedor);
    }

    public class GuiaEntradaRepository : MasterDataAccess, IGuiaEntradaRepository
    {
        public List<GUIA_ENTRADA> GetAllFromGuiaEntrada()
        {
            var hoy = DateTime.Today.Date;
            var query = from c in Context.GUIA_ENTRADA.Include("PROVEEDOR").Include("AREA.TRABAJADOR")//.Include("DETALLE_GUIA_ENTRADA.PRODUCTO")
                        orderby c.Fecha descending
                        //where c.Fecha == hoy
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();

        }

        public List<GUIA_ENTRADA> GetFromGuiaEntradaByCriterioAndAreaAndFecha(string proveedor, string nroDoc, string area, DateTime? fec1, DateTime? fec2)
        {
            var query = from h in Context.GUIA_ENTRADA.Include("PROVEEDOR").Include("AREA.TRABAJADOR")//.Include("DETALLE_GUIA_ENTRADA.PRODUCTO")
                        select h;

            if ((proveedor != "" && proveedor != null) || (nroDoc != "" && nroDoc != null))
            {
                if (area != "" && area != null) 
                {
                    if (fec1 != null && fec2 != null)
                    {
                        query = from h in query
                                where (h.FechaEntrada >= fec1.Value && h.FechaEntrada <= fec2.Value) &&
                                (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.NroDoc.ToUpper().Contains(nroDoc.ToUpper()))
                                        && h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
                                orderby h.Fecha descending
                                select h;

                        ObjectContext.Refresh(RefreshMode.StoreWins, query);
                    }
                    else
                    {
                        query = from h in query
                                where (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.NroDoc.ToUpper().Contains(nroDoc.ToUpper()))
                                        && h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
                                orderby h.Fecha descending
                                select h;

                        ObjectContext.Refresh(RefreshMode.StoreWins, query);
                    }
                }
                else if (fec1 != null && fec2 != null)
                {
                    query = from h in query
                            where (h.FechaEntrada >= fec1.Value && h.FechaEntrada <= fec2.Value)
                            && (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                            h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
                            h.NroDoc.ToUpper().Contains(nroDoc.ToUpper()))
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);
                }
                else 
                {
                    query = from h in query
                            where h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                                    h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
                                    h.NroDoc.ToUpper().Contains(nroDoc.ToUpper())
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);
                }
            }

            ///////////////////////////////////////

            if (area != "" && area != null)
            {
                if ((proveedor != "" && proveedor != null) || (nroDoc != "" && nroDoc != null)) 
                {
                    if (fec1 != null && fec2 != null)
                    {
                        query = from h in query
                                where (h.FechaEntrada >= fec1.Value && h.FechaEntrada <= fec2.Value) &&
                                (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.NroDoc.ToUpper().Contains(nroDoc.ToUpper()))
                                        && h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
                                orderby h.Fecha descending
                                select h;

                        ObjectContext.Refresh(RefreshMode.StoreWins, query);
                    }
                    else
                    {
                        query = from h in query
                                where (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
                                        h.NroDoc.ToUpper().Contains(nroDoc.ToUpper()))
                                        && h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
                                orderby h.Fecha descending
                                select h;

                        ObjectContext.Refresh(RefreshMode.StoreWins, query);
                    }
                }
                else if (fec1 != null && fec2 != null)
                {
                    query = from h in query
                            where (h.FechaEntrada >= fec1.Value && h.FechaEntrada <= fec2.Value)
                            && h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);
                }
                else 
                {
                    query = from h in query
                            where h.AREA.Descripcion.ToUpper().Contains(area.ToUpper()) 
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);
                }
            }

            ////////////////////////////////////////

            return query.ToList();


        }

        public GUIA_ENTRADA GetFromGuiaEntradaById(int id)
        {
            var query = from c in Context.GUIA_ENTRADA.Include("DETALLE_GUIA_ENTRADA.PRODUCTO.UNIDAD_MEDIDA") //.UNIDAD_MEDIDA
                        where c.ID.Equals(id)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddGuiaEntrada(GUIA_ENTRADA guiaEntrada)
        {
            foreach (var item in guiaEntrada.DETALLE_GUIA_ENTRADA)
            {
                Context.Entry(item.PRODUCTO).State = EntityState.Unchanged;
                Context.Entry(item).State = EntityState.Added;
            }

            if (guiaEntrada.PROVEEDOR != null)
            {
                Context.Entry(guiaEntrada.PROVEEDOR).State = EntityState.Unchanged;
                //Context.Entry(guiaEntrada.AREA).State = EntityState.Unchanged;
            }

            Context.GUIA_ENTRADA.Add(guiaEntrada);
            Context.SaveChanges();
        }

        public void UpdateGuiaEntrada(GUIA_ENTRADA guiaEntrada)
        {
            var estadosDetalle = Context.ChangeTracker.Entries<GUIA_ENTRADA>();

            guiaEntrada.PROVEEDOR = null;
            guiaEntrada.AREA = null;
            //Context.Entry(guiaEntrada.PROVEEDOR).State = EntityState.Unchanged;
            //Context.Entry(guiaEntrada.AREA).State = EntityState.Unchanged;

            Context.Entry(guiaEntrada).State = EntityState.Modified;

            foreach (var item in guiaEntrada.DETALLE_GUIA_ENTRADA)
            {
                item.PRODUCTO = null;

                Context.Entry(item).State = item.ID == Int32.Parse("0") ? System.Data.EntityState.Added : System.Data.EntityState.Modified;
                item.IdProducto = item.IdProducto;
            }

            Context.SaveChanges();
        }

        public bool GetAllGuiaEntradasByProveedorId(int idProveedor)
        {
            var query = from c in Context.GUIA_ENTRADA.Include("PROVEEDOR").Include("AREA")
                        where c.IdProveedor.Equals(idProveedor)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }
    }
}



//else if (area != "" && area != null)
//{
//    if ( (proveedor != "" && proveedor != null ) || (nroDoc != "" && nroDoc != null) )  //solo criterio
//    {
//        query = from h in query
//                where (h.PROVEEDOR.RazonSocialNombre.ToUpper().Contains(proveedor.ToUpper()) ||
//                        h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) ||
//                        h.NroDoc.ToUpper().Contains(nroDoc.ToUpper()))
//                        && h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
//                orderby h.Fecha descending
//                select h;

//        ObjectContext.Refresh(RefreshMode.StoreWins, query);
//    }
//    else if (fec1 != null && fec2 != null)//rango fechas 
//    {
//        query = from h in query
//                where (h.FechaEntrada >= fec1.Value && h.FechaEntrada <= fec2.Value) &&
//                h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
//                orderby h.Fecha descending
//                select h;

//        ObjectContext.Refresh(RefreshMode.StoreWins, query);
//    }
//    else //solo area
//    {
//        query = from h in query
//                where h.AREA.Descripcion.ToUpper().Contains(area.ToUpper())
//                orderby h.Fecha descending
//                select h;

//        ObjectContext.Refresh(RefreshMode.StoreWins, query);

//    }
//}
//else if (fec1 != null && fec2 != null)//
//{
//    if (area != "" && area != null) //solo area
//    {

//    }
//    if ((proveedor != "" && proveedor != null) || (nroDoc != "" && nroDoc != null))  //solo criterio
//    {

//    }
//    else
//    {
//    }
//}